<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use app\admin\model\Node as NodeModel;
use think\Cache;


class Office extends Admin
{

    //使用redis加载页面，
    public function supplies(){

        $res = Cache::get('name');

        if (empty($res)){
            $redis = new \Redis();
            $res= db('office_supplies')->select();
            $redis->connect('127.0.0.1', 6379);
            Cache::store('redis')->set('name',$res);
        }
        return view('admin@office/index',
              [
             'list' => $res
              ]);

      }

    //获取一条数据，放入页面
    public function getOffice($id)
    {
         $list= db('office_supplies')->where('id',$id)->find();
//         var_dump($list);exit;
        return view('admin@office/office_get',
            [
                'list' => $list
            ]);
    }
    //更新一条数据
    public function updateOffice($id)
    {
        $data = $_POST;
        $result = db('office_supplies')->where('id',$id)->update($data);

        if($result)
        {
            Cache::rm('name');
            $info['status']=true;
        }else{
            $info['status']=false;
        }
        return  json($info);
    }
    //加载添加页面
    public function addOffice()
    {
        return  view('admin@office/form');
    }
    //添加一条数据到数据库。
    public function suppliesData()
    {
      $data = $_POST;

        $officeid = db('office_supplies')->insert($data);
        if ($officeid){
            Cache::rm('name');
            $this->success('办公用品添加成功','admin/Office/supplies');
        }else{
            $this->error('办公用品添加失败','admin@office/suppliesData');
        }
    }
     //查询一条数据
    public function outbound($id)
    {

        $list= db('office_supplies')->where('id',$id)->find();
        return view('admin@office/outbound',
            [
                'list' => $list
            ]);
    }
    //删除一条数据
    public function delectOffice($id)
    {
        $result = db('office_supplies')->where(['id'=>$id])->delete();
        Cache::rm('name');
        if ($result) {
            $info['status'] = true;
        }else{
            $info['status'] = false;
        }
        return  json($info);
    }

}
